package com.cskaoyan.connectionpool;

import com.cskaoyan.util.JDBCUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.LinkedList;

/**
 * 创建日期: 2022/07/01 11:01
 *
 * @author ciggar
 *
 * 第一个版本
 */
public class MyConnectionPoolV1 {

    // 从头部存，从尾部取
    static LinkedList<Connection> pool;

    // 初始化大小
    static int INIT_SIZE = 10;

    // 初始化pool
    static {

        pool = new LinkedList<>();

        for (int i = 0; i < INIT_SIZE; i++) {

            Connection connection = JDBCUtils.getConnection();
            pool.addFirst(connection);
        }


    }


    // 获取连接
    public static Connection getConnection(){

        Connection connection = pool.removeLast();

        return connection;

    }

    // 返回连接
    public static void returnConnection(Connection connection){

        pool.addFirst(connection);
    }


}
